import pymysql

connect = pymysql.connect(host='localhost', user='root', passwd='li2843585', db='house_info')
cursor = connect.cursor()

cursor.execute("SELECT area,rent_money,location FROM lianjia_house_info;")

def mapify(num,min,max):
    #均一化
    return int(num*100/(max-min))


def formatLocation(data):
    #输入的是经纬度,格式化成小数点不超过3位的数据
    #返回字符串
    if data.find('.')<=0:
        return data
    intenger,decimal=data.split('.')
    if len(decimal)<2:
        return data
    return intenger+'.'+decimal[0:2]
a=[]
b={}
numrows = int(cursor.rowcount)
for i in range(numrows):
    row = cursor.fetchone()
    area=row[0].split('：')[1][:-1]
    if int(row[1])<=0 or int(area)<=0:
        continue
    long,lan=row[2].split(',')
    long,lan=formatLocation(long),formatLocation(lan)
    if long+','+lan in b.keys():
        b[long+','+lan]+=int(row[1])/int(area)
    else:
        b[long+','+lan]=int(row[1])/int(area)
ma=max(b.values())
mi=min(b.values())
for k,v in b.items():
    a.append([mapify(v,mi,ma),k])




with open('1.json','w',encoding='utf-8') as f:
    f.write('[')
    for i in a:
        f.write('[')
        f.write(i[1])
        f.write(',')
        f.write(str(i[0]))
        f.write(']')
        f.write(',')
    f.write(']')
